<template>
    <a-modal v-model:visible="visible" :closable="false" :width="800" :wrap-style="{ overflow: 'hidden' }"
        @ok="onClose">
        <a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
            <a-row :gutter="24">

                <a-col :span="16">
                    <a-form-item name="name">
                        <a-input v-model:value="searchFormState.name" placeholder="请输入检索条件" />
                    </a-form-item>
                </a-col>

                <a-col :span="8" class="text-right">
                    <a-button type="primary" @click="tableRef.refresh(true)">查询</a-button>
                </a-col>
            </a-row>
        </a-form>
        <s-table ref="tableRef" rowKey="id" tableLayout="fixed" :row-selection="{
        type: 'radio',
        selectedRowKeys: selectedRowKeys, onChange: onSelectChange
    }" :size="5" :data="loadData" bordered :columns="columns" class="mb-2">
            <template #bodyCell="{ column, record, index }">
                <template v-if="column.key === 'index'">
                    {{ index + 1 }}
                </template>
            </template>
        </s-table>
    </a-modal>
</template>
<script setup>
import api from '@/api/base_data/customerApi'
// import api from '@/api/contract/revenueContract/index'
import { ref } from 'vue';
const tableRef = ref()

const props = defineProps({
    value: {
        type: Array,
    },
})
const visible = ref(false)
const show = () => {
    nextTick(() => {
        visible.value = true
        if (props.value) {
            selectedRowKeys.value = props.value.map((item) => {
                return item.id
            })
            selectRecords.value = props.value
            tableRef.value.updateSelect(selectedRowKeys, props.value)
        }
    })
}

const searchFormState = ref({})

const loadData = (parameter) => {
    searchFormState.value.current = parameter.current
    searchFormState.value.size = parameter.size
    return api.getPage(searchFormState.value).then((res) => {
        return res
    })
}

const selectedRowKeys = ref([])
const selectRecords = ref({})
const onSelectChange = (selectedKeys, selectedRows) => {
    selectedRowKeys.value = selectedKeys;
    selectRecords.value = selectedRows
    console.log('selectedRowKeys changed: ', selectedRowKeys);
}


const emits = defineEmits(['onClose'])
const onClose = () => {
    visible.value = false;
    emits('onClose', selectRecords.value)
};
defineExpose({
    show
});
const refresh = () => {


}
const columns = [
    {
        title: '客户名称',
        dataIndex: 'custName',
        key: 'custName',
        width: 200,
    },
    {
        title: '行业',
        dataIndex: 'industry',
        key: 'industry',
    },
    {
        title: '联系人',
        dataIndex: 'contacts',
        key: 'contacts',
    },
    {
        title: '联系电话',
        dataIndex: 'contactsPhone',
        key: 'contactsPhone',
    },
]

</script>